package com.liuzhongni.wahahanews.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.adapter.ListViewAdapterOfSpecial;
import com.liuzhongni.wahahanews.headlineclass.special.SpacialFirst;
import com.liuzhongni.wahahanews.headlineclass.special.SpacialSecond;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;


/**
 * Created by dllo on 16/1/11.
 */
public class SpacialActivity extends AppCompatActivity  {
    private ExpandableListView listView;
    private TextView textViewSpacialDigest;
    private NetworkImageView networkImageView;
    private ListViewAdapterOfSpecial adapter;
    private SpacialFirst spacialFirst = new SpacialFirst();
    private SpacialSecond spacialSecond  = new SpacialSecond();
    private int[] textViewsID = {R.id.spacial_tv1, R.id.spacial_tv2, R.id.spacial_tv3, R.id.spacial_tv4, R.id.spacial_tv5, R.id.spacial_tv6, R.id.spacial_tv7, R.id.spacial_tv8,
            R.id.spacial_tv9, R.id.spacial_tv10, R.id.spacial_tv11, R.id.spacial_tv12};
    private TextView[] textViews = new TextView[12];
    private String skipType;
    public  static  String specialID;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spacial);

        listView = (ExpandableListView) findViewById(R.id.expandable_special_lv);
        getJson();


    }

    private void init() {

        spacialSecond = spacialFirst.getSpacial();
        adapter = new ListViewAdapterOfSpecial(spacialSecond, this);
        View headView = LayoutInflater.from(this).inflate(R.layout.expandablelistview_headview, null);
        textViewSpacialDigest = (TextView) headView.findViewById(R.id.headview_expandable_tv_digest);
        networkImageView = (NetworkImageView) headView.findViewById(R.id.headview_expandable_iv);
        for (int i = 0; i < textViewsID.length; i++) {
            textViews[i] = (TextView) headView.findViewById(textViewsID[i]);
        }
        if (spacialSecond.getTopics().size() < 12) {
            for (int i = 0; i < 12 - spacialSecond.getTopics().size(); i++) {
                textViews[i + spacialSecond.getTopics().size()].setVisibility(View.INVISIBLE);
            }
        }
        for (int i = 0; i < spacialSecond.getTopics().size(); i++) {
            Log.d("SpacialActivity", "spacialSecond.getTopics().size():" + spacialSecond.getTopics().size());
            textViews[i].setText(spacialSecond.getTopics().get(i).getTname());
            final int finalI = i;
            textViews[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listView.setSelectedGroup(finalI);
                }
            });
        }
        textViewSpacialDigest.setText("         " + spacialSecond.getDigest());
        VolleySingleton.getOurInstance().imgJson(networkImageView, spacialSecond.getBanner());
        listView.addHeaderView(headView);
        listView.setAdapter(adapter);
        listView.setGroupIndicator(null);
        for(int i = 0; i < adapter.getGroupCount(); i++){

            listView.expandGroup(i);

        }
    }

    private void getJson() {
        Intent intent = getIntent();
        skipType = intent.getStringExtra("skipType");
        specialID = intent.getStringExtra("specialID");
        Log.d("SpacialActivity", specialID);
        String url = "http://c.3g.163.com/nc/" + skipType + "/" + specialID + ".html";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Log.d("SpacialActivity", response);
                response = response.replaceFirst(specialID,"Spacial");
                Log.d("SpacialActivity", response);
                spacialFirst = gson.fromJson(response, SpacialFirst.class);
                init();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("SpacialActivity", "失败");
            }
        });
        VolleySingleton.getOurInstance().addRequest(request);
    }



}

